/**
 * 
 */
package name.yzhu.httpserver.handle;

import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import com.google.common.base.Joiner;

import name.yzhu.common.CommonConstant;
import name.yzhu.httpserver.HttpRequestType;
import name.yzhu.httpserver.HttpReturnData;
import name.yzhu.httpserver.IHttpPermission;
import name.yzhu.httpserver.log.BaseLogHandler;
import name.yzhu.httpserver.log.LogEntity;
import name.yzhu.httpserver.log.LogHandler;

/**
 * @author yhzhu
 *
 */
public class RootURLHandleProcess implements HandleURLProcess{

	private List<URLHandleToMethod> handleList = null;

	public RootURLHandleProcess(URLHandle urlHandle) {
		handleList  = urlHandle.getHandles();
	}

	@Override
	public String getPattern() {
		return null;
	}

	@Override
	public HttpReturnData getExceptionData(int num, String msg) {
		return null;
	}

	@Override
	public LogHandler getLogHandler(HttpRequestType type) {
		return new BaseLogHandler();
	}

	@Override
	public void paraseRetData(HttpReturnData retData, LogEntity entity) {
		
	}

	@Override
	public IHttpPermission getHttpPermissionFactory() {
		return new IHttpPermission(){

			@Override
			public boolean check(HttpServletRequest request,
					HttpReturnData retData) {
				return true;
			}};
	}

	@URLHandleAnno(nativeReqeust = true, requestType = 1)
	public HttpReturnData get(HttpServletRequest req){
		HttpReturnData data = new HttpReturnData();
		StringBuilder sb = new StringBuilder();
		sb.append("<table border=\"1\">");
		sb.append("<tr>");
		sb.append("<th>Type</th>");
		sb.append("<th>URL</th>");
		sb.append("<th>Parameters</th>");
		sb.append("<th>RequiredParameters</th>");
		sb.append("</tr>");
		for (URLHandleToMethod  handle: handleList){
			HandleURLProcess process = handle.getProcess();
			
			String pattern = process.getPattern();
			if (pattern.startsWith("^"))
				pattern = pattern.substring(1);
			if (pattern.endsWith("$"))
				pattern = pattern.substring(0, pattern.length() - 1);
			String url="http://"+req.getLocalName()+":"+req.getLocalPort()+pattern;
			List<String> params = handle.getHmd().getParameterNames();
			int extSize = handle.getHmd().getExtParameterNames().size();
			Class<?>[] type = handle.getParamsTypes();
			StringBuilder sbParam = new StringBuilder();
			for(int i=0; i<params.size(); i++){
				if (i > 0)
					sbParam.append(", ");
				String p = params.get(i);
				sbParam.append("<b>"+type[i+extSize].getSimpleName()+"</b> "+p);
			}
			Set<String> required = handle.getHmd().getRequiredParam();
			String reqs="";
			if (required != null && required.size() > 0){
				reqs = Joiner.on(",").join(required);
			}
			sb.append("<tr>");
			sb.append("<td>"+handle.getHmd().getRequestType()+"</td>");
			sb.append("<td>"+url+"</td>");
			sb.append("<td>"+sbParam.toString()+"</td>");
			sb.append("<td>"+reqs+"</td>");
			sb.append("</tr>");
		}
		sb.append("</table>");
		data.setData(sb.toString().getBytes(CommonConstant.CHARSET_UTF_8));
		return data;
	}
}
